package com.gmail.yesguxun.tumblr.content;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import com.gmail.yesguxun.tumblr.Constant;
import com.gmail.yesguxun.tumblr.ui.ViewBigPhotoActivity;
import com.gmail.yesguxun.util.EventBusAsync;
import com.gmail.yesguxun.util.EventBusEventObj;
import com.gmail.yesguxun.util.LogFactory;
import com.gmail.yesguxun.util.LogX;

import java.io.BufferedInputStream;

public class TrContentObject {
    private LogX mLog = LogFactory.getLogObject();
    
	String mPropertyRemoteUrl = null;       
    String mPropertyPostId = null;
    String mPropertyReblogKey = null;
    
    boolean mLocalCopyExist = false;
    int mObjId = 0;
    
    
    public TrContentObject(String objUri)
    {
        mPropertyRemoteUrl = objUri;
        mObjId = TrContentObjectPool.createContentId();
        mLocalCopyExist = false;
    }

    
    public String getURI() {
        
        //return mPropertyRemoteUrl;
    	return getLocalName();
    }
    
    
    public int getID() {
        
        return mObjId;
    }
    
    public String getLocalName()
    {
    	File dataFolder = ViewBigPhotoActivity.getContextWrapper().getFilesDir();
    	File file = new File(dataFolder, getTitle());    	    	
    	
    	return file.getAbsolutePath();
    }
    
    public String getTitle()
    {
        return mPropertyPostId + "_" + mObjId + ".jpg";
    }
    
    
    
    public boolean localCopyExist()
    {
    	return this.mLocalCopyExist;
    }
    
    public void downloadLocal() throws MalformedURLException, IOException
    {
    	int bufferSize = 1024 * 3;
    	
    	BufferedInputStream in = new BufferedInputStream(new java.net.URL(this.mPropertyRemoteUrl).openStream());
    	java.io.FileOutputStream fos = new java.io.FileOutputStream(this.getURI());

    	java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,bufferSize);
    	byte[] data = new byte[bufferSize];
    	int x=0;
    	while((x=in.read(data,0,bufferSize))>=0)
    	{
    		bout.write(data,0,x);
    	}
    	
    	bout.close();
    	in.close();	
    	
    	mLocalCopyExist = true;
    	EventBusAsync.getInstance().fireEvent(new EventBusEventObj(Constant.EVENT_NEW_CONTENT_DOWNLOAD_FINISHED,this));
    }
    
    public void downloadLocalAsync()
    {
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
                try {
                    downloadLocal();
                } catch (MalformedURLException e) {                    
                    e.printStackTrace();
                } catch (IOException e) {                    
                    e.printStackTrace();
                }
            }        
        };
        
        new Thread(runnable).start();
    }
    
    public String getPropertyString(String propName)
    {
    	String propValue = null;
    	if(propName.equals(Constant.CONTENT_PROPERTY_POST_ID))
    	{
    		propValue = this.mPropertyPostId;
    	}
    	else if(propName.equals(Constant.CONTENT_PROPERTY_REMOTE_URL))
    	{
    		propValue = this.mPropertyRemoteUrl;
    	}
    	else if(propName.equals(Constant.CONTENT_PROPERTY_REBLOG_KEY))
        {
            propValue = this.mPropertyReblogKey;
        }
    		
        return propValue;
    }
    
    public boolean setProperty(String propName, String propValue)
    {
    	boolean success = false;
    	
    	if(propName.equals(Constant.CONTENT_PROPERTY_POST_ID))
    	{
    		this.mPropertyPostId = propValue;
    		success = true;
    	}
    	else if(propName.equals(Constant.CONTENT_PROPERTY_REMOTE_URL))
    	{
    		this.mPropertyRemoteUrl = propValue;
    		success = true;
    	}
    	else if(propName.equals(Constant.CONTENT_PROPERTY_REBLOG_KEY))
        {
            this.mPropertyReblogKey = propValue;
            success = true;
        }
    	
    	return success;
    }        
}
